//
//  SaveToPhotosMediaShareCommunity.m
//  MediaShareBB
//
//  Created by Artak Gevorgyan on 7/15/13.
//  Copyright (c) 2013 InnoriseEntertainment. All rights reserved.
//

#import "SaveToPhotosMediaShareCommunity.h"
#import "MediaShareData.h"
#import "MediaShareBBUtilityMethods.h"

#import "ALAssetsLibrary+CustomPhotoAlbum.h"
#import <MessageUI/MessageUI.h>

@interface SaveToPhotosMediaShareCommunity()
{
	MediaShareData* _sharedMediaData;
	NSString* _albumName;
}

@end

@implementation SaveToPhotosMediaShareCommunity

-(id) init {
	self = [super init];
	if (self) {
		super.name = @"Save To Photos";
		super.communityType = MEDIA_SHARE_COMMUNITY_SAVE_TO_PHOTOS;
		super.supportedMediaTypes = [[NSArray alloc] initWithObjects:
									 [NSNumber numberWithInt:IMAGE_MEDIA_TYPE],
									 [NSNumber numberWithInt:VIDEO_MEDIA_TYPE],
									 nil];
	}
	return self;
}

#pragma mark - Superclass methods

// Superclass method
-(NSString*) description {
	NSString* d = @"Save To Photos description";
	return d;
}

// Superclass method
-(void) configureWithCredentials:(NSDictionary*) credentials {
	_albumName = [credentials valueForKey:@"albumName"];
}

// Superclass method
-(void) login {
	if ([MediaShareBBUtilityMethods isObject:super.delegate
						  respondsToSelector:@selector(loginSuccess)]) {
		[super.delegate loginSuccess];
	}
}

// Superclass method
-(void) shareMedia:(MediaShareData*) data {
	if ([super isValidMediaType:data.mediaType]) {
		_sharedMediaData = data;
		if ([MediaShareBBUtilityMethods isObject:super.delegate
							  respondsToSelector:@selector(willSharingMedia:through:)]) {
			[super.delegate willSharingMedia:_sharedMediaData
									 through:self];
		}
        if (data.mediaType == IMAGE_MEDIA_TYPE) {
            [self savePhotoToGallery];
        } else if (data.mediaType == VIDEO_MEDIA_TYPE) {
            [self saveVideoToGalery];
        }
	} else {
		NSLog(@"%s: Unsupported media type:%d", __PRETTY_FUNCTION__, data.mediaType);
	}
}

#pragma mark - Private methods

-(void) savePhotoToGallery {
	ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
	[library saveImage:[MediaShareBBUtilityMethods imageFromData:_sharedMediaData.data]
			   toAlbum:_albumName
   withCompletionBlock:^(NSError *error) {
		   [self savingCompleted:error];
       }];
}

-(void) saveVideoToGalery {
    NSURL *url = [NSURL fileURLWithPath:_sharedMediaData.path];
    ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:url
                                completionBlock:^(NSURL *assetURL, NSError *error){
                                    [self savingCompleted:error];
                                }];
}

-(void) savingCompleted:(NSError*) error {
    if (error != nil) {
        NSLog(@"%s: Error: %@", __PRETTY_FUNCTION__, [error description]);
        if ([MediaShareBBUtilityMethods isObject:super.delegate
                              respondsToSelector:@selector(didFailedSharingMedia:through:withError:)]) {
            [super.delegate didFailedSharingMedia:_sharedMediaData
                                          through:self
                                        withError:error];
        }
    } else {
        if ([MediaShareBBUtilityMethods isObject:super.delegate
                              respondsToSelector:@selector(didFinishSharingMedia:through:)]) {
            [super.delegate didFinishSharingMedia:_sharedMediaData
                                          through:self];
        }
    }
    [super.delegate communityViewWillClose];
}

@end
